/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rcs.folders;

/**
 *
 * @author pablo
 */
public class BasicItemsSystem implements VirtualItemsSytem {

  private VirtualFolder rootFolder;

  public BasicItemsSystem(VirtualFolder rootFolder) {
    this.rootFolder = rootFolder;
  }

  public VirtualFolder getRootFolder() {
    return rootFolder;
  }

  @Override
  public VirtualItem getVirtualItem(String uri) {
    VirtualItem result = null;
    String[] paths = splitPath(uri);

    if (uri.length() > 0 && paths.length > 1) {
      int i = 1;
      VirtualItem current = rootFolder;
      boolean exit = (null == current || !(current instanceof VirtualFolder));

      while (!exit) {
        current = ((VirtualFolder) current).childFromDisplayName(paths[i]);
        ++i;

        if (paths.length == i)
          result = current;

        exit = !(i < paths.length) || !(current instanceof VirtualFolder);
      }
    } else
      result = rootFolder;

    return result;
  }

  public String[] splitPath(String uri) {
    return uri.split("/");
  }
}
